/*	
	stencil - action script 3 isometric game engine.

	http://code.google.com/p/stencilas3/
	
	Copyright (c) 2010 Max Zhuravkov, All Rights Reserved.

	Permission is hereby granted, free of charge, to any person obtaining a copy of
	this software and associated documentation files (the "Software"), to deal in
	the Software without restriction, including without limitation the rights to
	use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
	of the Software, and to permit persons to whom the Software is furnished to do
	so, subject to the following conditions:

	The above copyright notice and this permission notice shall be included in all
	copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
	SOFTWARE.
*/
package stencil.core.engine.scene 
{
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	import stencil.core.engine.Bounds;
	import stencil.core.engine.IMapGraph;
	import stencil.core.engine.ISceneObject;
	import stencil.core.engine.MapNode;
	import stencil.core.engine.Position;
	import stencil.core.engine.SceneObjectGrid;
	import stencil.core.engine.SimpleMapGraph;
	
	/**
	 * 
	 * @author Max Zhuravkov
	 */
	public class EmptySceneStructure extends GridBasedSceneStructure {
		
		private var _objectGrid:SceneObjectGrid;
		
		private var _map:Dictionary = new Dictionary();
		
		//------------------------------------------------------------------------------------------------
		// Constructor
		//------------------------------------------------------------------------------------------------
		
		public function EmptySceneStructure(boundaries:Rectangle) {
			super(boundaries);
		}
		
		//------------------------------------------------------------------------------------------------
		// Public methods
		//------------------------------------------------------------------------------------------------
		
		/**
		 * @inheritDoc
		 */
		override public function init():void {
			super.init();
			
			if(this.mapGraph == null){
				this.mapGraph = this.createMapGraph();
			}
			
			this._objectGrid = new SceneObjectGrid();
			this._objectGrid.addCallback(this.gridUpdateCallback);
			
			var width:int = this.boundaries.width;
			var height:int = this.boundaries.height;
			var tileSectionWidth:int = this.tileSettings.sectionWidth;
			var tileSectionHeight:int = this.tileSettings.sectionHeight;
			var sectionColumnCount:int = width / tileSectionWidth;
			var sceneNode:SimpleSceneNode;
			var tiles:Array = new Array(width * height);
			var sceneNodes:Array = new Array(width / tileSectionWidth * height / tileSectionHeight)
			
			for (var i:int = 0; i < width; i ++ ) {
				var sx:int = Math.floor(i / tileSectionWidth) * tileSectionWidth;
				var si:int = Math.floor(i / tileSectionWidth)
				
				for (var j:int = 0; j < height; j ++ ) {
					var sy:int = Math.floor(j / tileSectionHeight) * tileSectionHeight;
					var sj:int = Math.floor(j / tileSectionHeight)
					var index:Number = si + sj * sectionColumnCount;
					
					sceneNode = sceneNodes[index];
					
					if (!sceneNode) {
						
						sceneNode = new SimpleSceneNode();
						sceneNode.rect = new Rectangle(i, j, tileSectionWidth, tileSectionHeight);
						sceneNode.id = sx + "_" + sy;
						sceneNode.bounds = new Bounds(this.tileGrid.getRect(sceneNode.rect));
						sceneNode.tiles = [];
						sceneNode.position = new Position(i, j);
						sceneNode.objects = [];
						
						this.sceneNodeCreated(sceneNode);
						
						sceneNodes[index] = sceneNode;
						this.addObject(sceneNode);
					}
					
					
					var node:MapNode = this.createMapNode(sceneNode);
					
					node.x = i;
					node.y = j;
					
					this.mapGraph.addNode(node);
					
					sceneNode.tiles.push(node);
				}
			}
		}
		
		/**
		 * @inheritDoc
		 */
		override public function getObjectsAt(p:Position, filter:Function = null):Array {
			return this._objectGrid.getObjectsAt(p, filter);
		}
		
		//------------------------------------------------------------------------------------------------
		// Protected methods
		//------------------------------------------------------------------------------------------------
		
		protected function createMapGraph():IMapGraph {
			return new SimpleMapGraph(this.boundaries);
		}
		
		protected function sceneNodeCreated(sceneNode:ISceneNode):void {
			
		}
		
		protected function createMapNode(sceneNode:ISceneNode):MapNode {
			var node:MapNode = new MapNode();
			node.texture = -1
			node.walkable = true;
			return node;
		}
		
		override protected function objectAdded(o:ISceneObject):void {
			super.objectAdded(o);
			
			this._objectGrid.putOrUpdate(o);
		}
		
		override protected function objectRemoved(o:ISceneObject):void {
			super.objectRemoved(o);
			
			this._objectGrid.remove(o);
		}
		
		protected function gridUpdateCallback(p:Position, o:ISceneObject, put:Boolean):void {
			if (o is ISceneNode) {
				return;
			}
			
			var key:int = p.y * this.boundaries.width + p.x;
			var node:MapNode = this.mapGraph.getNodeAt(p);
			
			if (node == null) {
				return;
			}
			
			if (put) {
				node.walkable = false;
				this._map[key] = true; 
				
			}else {
				this._map[key] = false;
				node.walkable = true;
				delete this._map[key];
			}
		}
	}

}